<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/23
 * Time: 17:51
 */

//管理员添加团队信息接口
require_once 'db.php';
require_once 'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$fileInfo = isset($_FILES['img']) ? $_FILES['img'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$content = isset($_POST['content']) ? $_POST['content'] : '';

//var_dump($fileInfo);
//var_dump($name);
//var_dump($content);
//exit();
if(empty($fileInfo) || empty($name) || empty($content)) {
    return Response::show(401,'数据不合法');
}

try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

$sql = "INSERT team VALUES(null,?,?,null,1)";

$mysqli_stmt = $connect->prepare($sql);
$mysqli_stmt->bind_param('ss',$name,$content);

$data = array(
    'IsSuccess' => false,
    'InsertId' => '',
    'ImgSuccess' => false,
    'ImgFalseRes' => ''
);

if($mysqli_stmt->execute()) {
    $data['InsertId'] = $mysqli_stmt->insert_id;
} else {
    return Response::show(404,'团队信息上传失败，服务器没响应',$data);
}

//判断文件上传的错误号
if($fileInfo['error']>0) {
    //匹配错误信息
    $mes = '';
    switch ($fileInfo['error']){
        case 1:
            $mes = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
            break;
        case 2:
            $mes = '超过了表单MAX_FILE_SIZE限制的大小';
            break;
        case 3:
            $mes = '文件部分被上传';
            break;
        case 4:
            $mes = '没有选择上传文件';
            break;
        case 6:
            $mes = '没有找到临时目录';
            break;
        case 7:
        case 8:
            $mes = '系统错误';
    }
//    exit($mes);
    $data['ImgFalseRes'] = $mes;
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}
//获得文件的扩展名
$ext = pathinfo($fileInfo['name'],PATHINFO_EXTENSION);

//规定允许上传的文件类型
$allowExt = array('jpeg','jpg','png');
if(!is_array($allowExt)) {
//    exit('系统错误');
    $data['ImgFalseRes'] = '系统错误';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}
//检测上传文件的类型
if(!in_array($ext,$allowExt)) {
//    exit('非法文件类型');
    $data['ImgFalseRes'] = '非法文件类型';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}

$maxSize  = '2097152';
//检测上传文件大小
if($fileInfo['size']>$maxSize) {
//    exit('上传文件过大');
    $data['ImgFalseRes'] = '上传文件过大';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}

//    检测图片是否为真实的图片类型
if(!getimagesize($fileInfo['tmp_name'])) {
//    exit('不是真实的图片类型');
    $data['ImgFalseRes'] = '不是真实的图片类型';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}

//检测文件是否通过HTTP POST方式上传上来
if(!is_uploaded_file($fileInfo['tmp_name'])) {
//    exit('文件不是通过HTTP POST方式上传上来的');
    $data['ImgFalseRes'] = '文件不是通过HTTP POST方式上传上来的';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}

//进行文件的移动
$uploadPath = 'uploads';
if(!file_exists($uploadPath)) {
    mkdir($uploadPath,0777,true);
    chmod($uploadPath,0777);
}
//给文件进行命名，是根据时间生成一个唯一的文件名
$uniName = md5(uniqid(microtime(true),true)).'.'.$ext;
//$uniName = microtime(true).'.'.$ext;
$destination = $uploadPath.'/'.$uniName;

if(!@move_uploaded_file($fileInfo['tmp_name'],$destination)) {
//    exit('文件移动失败');
    $data['ImgFalseRes'] = '文件移动失败';
    return Response::show(400,'内容已经插入，图片上传失败',$data);
}

//return $destination;
//return Response::show(400,'',$data);
$mysqli_stmt->free_result();
$mysqli_stmt->close();

$sql = "UPDATE team SET team_img='$destination' WHERE team_id='$data[InsertId]'";

$res = $connect->query($sql);

if($res && $connect->affected_rows) {
    $data['IsSuccess'] = true;
    $data['ImgSuccess'] = true;
    return Response::show(200,'团队信息上传成功',$data);
} else {
    $data['ImgFalseRes'] = '图片储存路径插入数据库时失败';
    return Response::show(405,'内容已经插入，图片上传成功，入库失败',$data);
}
